package com.market.plus.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.market.plus.core.dao.ParticipantDao;
import com.market.plus.core.dao.TribeDao;
import com.market.plus.core.domain.Album;
import com.market.plus.core.domain.AlbumType;
import com.market.plus.core.domain.Avatar;
import com.market.plus.core.domain.News;
import com.market.plus.core.domain.NewsType;
import com.market.plus.core.domain.Participant;
import com.market.plus.core.domain.Status;
import com.market.plus.core.domain.Tribe;
import com.market.plus.core.dto.AvatarDto;
import com.market.plus.core.dto.NewsDto;
import com.market.plus.core.dto.TribeDto;
import com.market.plus.core.mapping.DTOService;
import com.market.plus.core.mapping.DomainService;
import com.market.plus.core.service.MarketOCMService;
import com.market.plus.core.service.TribeService;
import com.market.plus.core.util.MarketUtil;

@Service("tribeService")
@Transactional(readOnly = true)
public class TribeServiceImpl implements TribeService {

   private DomainService domainService;

   private DTOService dtoService;

   private TribeDao tribeDao;

   private ParticipantDao participantDao;

   private MarketOCMService marketOCMService;

   @Autowired
   public TribeServiceImpl(
       @Qualifier("tribeDao")TribeDao tribeDao,
       @Qualifier("participantDao")ParticipantDao participantDao,
       @Qualifier("marketDomainService") DomainService domainService,
       @Qualifier("marketOCMService") MarketOCMService marketOCMService,
       @Qualifier("marketDtoService") DTOService dtoService) {
      this.tribeDao = tribeDao;
      this.participantDao=participantDao;
      this.marketOCMService = marketOCMService;
      this.domainService = domainService;
      this.dtoService = dtoService;
   }

   @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
   public String saveTribe(TribeDto tribeDto, CommonsMultipartFile avatarFile, Long participantId) throws Exception{
      Tribe tribe = domainService.convertTribe(tribeDto);
      Participant participant = this.participantDao.findById(participantId);
      Album tribeAlbum = this.participantDao.findByAlbumType(participantId, AlbumType.TRIPE);
      if (tribeAlbum==null){
         tribeAlbum = new Album();
         tribeAlbum.setName("Avatar Hoi");
         tribeAlbum.setAlbumType(AlbumType.TRIPE);
         participant.addAlbum(tribeAlbum);
         MarketUtil.setModifyedUser(tribeAlbum, Status.UPDATED);
      }
      News news=new News();
      news.setTitle("");
      news.setContent(tribe.getDescription());
      news.setNewsType(NewsType.TRIBENEWS);
      news.setParticipant(participant);
      news.setTotalComments(0);
      MarketUtil.setModifyedUser(news, Status.UPDATED);
      tribe.addNews(news);

      String guid=this.marketOCMService.addTribe(participant, tribe, tribeAlbum, avatarFile);
      tribe.setTribeGUID(guid);
      tribe.setParticipant(participant);
      participant.addTribe(tribe);
      this.tribeDao.save(tribe);
      this.participantDao.save(participant);
      return guid;
   }

   public List<TribeDto> getTribes(String participantGUID){
      List<TribeDto> tribeDtos = this.marketOCMService.getTribes(participantGUID);
      List<TribeDto> expectedTribeDtos = new ArrayList<TribeDto>();
      for (TribeDto tribeDto:tribeDtos){
         Tribe tribe = this.tribeDao.findByGUID(tribeDto.getTribeGUID());
         if (tribe != null && tribe.getAvatar() != null){
            Avatar avatar = tribe.getAvatar();
            AvatarDto avatarDto = this.dtoService.convertAvatarDto(avatar, null);
            tribeDto.setAvatar(avatarDto);
            expectedTribeDtos.add(tribeDto);
         }
      }
      return expectedTribeDtos;
   }

   public TribeDto getNews(String tribeGUID) {
      String[] newsGUID = this.marketOCMService.getTribeNewGUIDs(tribeGUID);
      //List<News> news = this.tribeDao.getNews(tribeGUID);
      Tribe tribe = this.tribeDao.findByGUID(tribeGUID);
      TribeDto tribeDto = this.dtoService.convertTribeDto(tribe, null);
      List<NewsDto> newsDto = tribeDto.getNews();
      //List<NewsDto> newsDto = this.dtoService.convertNewsDto(news, null);
      List<NewsDto> orderableNewsDto = new ArrayList<NewsDto>();
      NewsDto expectedNewsDto = new NewsDto();
      for (String guid:newsGUID){
         expectedNewsDto.setGuid(guid);
         int index=newsDto.indexOf(expectedNewsDto);
         if (index>=0){
            orderableNewsDto.add(orderableNewsDto.size(),newsDto.get(index));
         }
      }
      tribeDto.setNews(orderableNewsDto);
      return tribeDto;
   }
}
